<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-select clearable style="width:150px" class="filter-item" v-model="listQuery.type" placeholder="消息类型">
        <el-option v-for="item in typeStatusMap" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button v-if="sys_message_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" stripe border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="消息类型">
        <template slot-scope="scope">
          <span>{{scope.row.type | typeFilter}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="接收人员">
        <template slot-scope="scope">
          <span>{{scope.row.userName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="sys_message_upd" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="sys_message_del" size="small" type="danger" @click="deletes(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">

        <el-form-item label="消息类型" prop="type">
          <el-select class="filter-item" v-model="form.type" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item" :label="item | typeFilter" :value="item"> </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="接收人员" prop="openId">
           <el-select class="filter-item" v-model="form.openId" filterable @change="getName" placeholder="请选择">
            <el-option v-for="item in allPersons" :key="item.openId" :label="item.userName" :value="item.openId"> </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="名称" v-if="isShow" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入名称"></el-input>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj, getUsers } from '@/api/message/message'
import waves from '@/directive/waves/index.js' // 水波纹指令
// import { parseTime } from '@/utils'
import { mapGetters } from 'vuex'

export default {
  name: 'table_message',
  directives: {
    waves
  },
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        type: ''
      },
      form: {
        userName: undefined,
        type: undefined,
        openId: undefined
      },
      rules: {
        openId: [
          {
            required: true,
            message: '请选择',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择',
            trigger: 'blur'
          }
        ]
      },
      typeOptions: [1, 2, 3, 4],
      dialogFormVisible: false,
      dialogStatus: '',
      allPersons: [],
      isShow: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      typeStatusMap: [
        {
          label: '维修上报消息',
          value: 1
        },
        {
          label: '接单成功消息',
          value: 4
        },
        {
          label: '维修完成消息',
          value: 2
        },
        {
          label: '巡视记录消息',
          value: 3
        }
      ],
      tableKey: 0
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  filters: {
    typeFilter(type) {
      const typeMap = {
        1: '维修上报消息',
        4: '接单成功消息',
        2: '维修完成消息',
        3: '巡视记录消息'
      }
      return typeMap[type]
    }
  },
  created() {
    this.getList()
    this.sys_message_add = this.permissions['sys_message_add']
    this.sys_message_upd = this.permissions['sys_message_upd']
    this.sys_message_del = this.permissions['sys_message_del']
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery.orderByField = ''
      this.listQuery.isAsc = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getName(val) {
      // locations是v-for里面的也是datas里面的值
      let obj = {}
      obj = this.allPersons.find((item) => {
        return item.openId === val
      })
      this.form.userName = obj.userName
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      getUsers().then(response => {
        this.allPersons = response.data
      })
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        getUsers().then(response => {
          this.allPersons = response.data
        })
        this.form = response.data
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
      })
    },
    create(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    cancel(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    update(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          putObj(this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    deletes(row) {
      this.$confirm(
        '此操作将永久删除,是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delObj(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .cache(() => {
            this.$notify({
              title: '失败',
              message: '删除失败',
              type: 'error',
              duration: 2000
            })
          })
      })
    },
    resetTemp() {
      this.form = {
        id: undefined,
        openId: undefined,
        type: undefined,
        userName: undefined
      }
    }
  }
}
</script>
